﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WarehouseManagement.BusinessLogic.BC;
using System.Linq;
using WarehouseManagement.BusinessLogic.DTO;

namespace WarehouseManagement.Views.Stock
{
    public partial class InputMerchandiseInStock : DevExpress.XtraEditors.XtraForm
    {
        private MerchandiseBC _merchandiseBC;
        private StockBC _stockBC;

        private BindingList<InputMerchandiseInStockDTO> _stockList;

        public InputMerchandiseInStock()
        {
            InitializeComponent();
            _stockList = new BindingList<InputMerchandiseInStockDTO>();
            _merchandiseBC = new MerchandiseBC();
            _stockBC = new StockBC();
            LoadMerchandiseLookupData();
        }

        private void InputMerchandiseInStock_Load(object sender, EventArgs e)
        {
            grdImportList.DataSource = _stockList;
            lblTitle.Text = string.Format("Tồn đầu kỳ tháng {0} năm {1}", DateTime.Today.Month, DateTime.Today.Year).ToUpper();
        }

        public IEnumerable<dynamic> MerchandiseList
        {
            get
            {
                return ipeMerchandiseLookup.DataSource as IEnumerable<dynamic>;
            }
            set
            {
                ipeMerchandiseLookup.DataSource = value;
                ipeMerchandiseLookup.NullText = "(Chọn mặt hàng)";
                ipeMerchandiseLookup.ValueMember = "MerchandiseId";
                ipeMerchandiseLookup.DisplayMember = "MerchandiseName";




            }
        }

        private void LoadMerchandiseLookupData()
        {
            var list = _merchandiseBC.GetMerchandiseList();

            MerchandiseList = list.Select(e => new
            {
                MerchandiseUnit = e.Unit,
                MerchandiseId = e.Id,
                e.MerchandiseName,
                QuantityInStock = _stockBC.GetViewStockInfo(e.Id, DateTime.Today, DateTime.Today).EndPeriodQuantity
            }).ToList();
        }

        private void ipeMerchandiseLookup_EditValueChanged(object sender, EventArgs e)
        {
            var edit = sender as GridLookUpEdit;
            //var view = edit.Properties.View as DevExpress.XtraGrid.Views.Grid.GridView;

            var fr = grvImportList.GetFocusedRow() as InputMerchandiseInStockDTO;

            var selectedItem = (dynamic)edit.GetSelectedDataRow();
            fr.MerchandiseUnit = selectedItem.MerchandiseUnit;


        }

        private void grvImportList_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {


            var obj = e.Row as InputMerchandiseInStockDTO;
            if (obj.MerchandiseId == 0)
            {
                e.Valid = false;
                e.ErrorText = "Bạn chưa nhập mặt hàng";
            }
            if (_stockList.Count(f => f.MerchandiseId == obj.MerchandiseId) > 1)
            {

                var msg = string.Format("Đã có mặt hàng này trong danh sách");
                XtraMessageBox.Show(msg, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                RemoveItem(obj);
            }

        }

        private void RemoveItem(InputMerchandiseInStockDTO item)
        {
            if (item != null)
                _stockList.Remove(item);
        }

        private InputMerchandiseInStockDTO GetFocusObject()
        {
            return grvImportList.GetFocusedRow() as InputMerchandiseInStockDTO;
        }
        private void grvImportList_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            var obj = GetFocusObject();
            btnDelete.Enabled = obj != null;
            btnSave.Enabled = _stockList.Count > 0;

        }

        private void grvImportList_DataSourceChanged(object sender, EventArgs e)
        {
           
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (XtraMessageBox.Show("Bạn có chắc muốn xóa dòng đã chọn?", "Xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                return;
            RemoveItem(GetFocusObject());
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                _stockBC.SaveMerchandiseInStockList(_stockList);
                btnSave.Enabled = btnDelete.Enabled = false;
                XtraMessageBox.Show("Đã lưu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show("Đã xảy ra lỗi. Bạn hãy thử lại sau. Lỗi: " + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}